package com.ygj.inspection.action;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.json.JsonUtil;
import com.base.common.persistent.model.Employee;
import com.cjy.common.bean.OpAttributes;
import com.ygj.inspection.service.InspectionService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import javax.annotation.Resource;
import java.util.List;

import static com.base.common.GlobaConstants.*;

/**
 * 巡查Action
 * 
 * @author Kevin Colin
 * @datetime 2015年9月5日下午4:02:14
 * 
 */
@Action("inspectionAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class InspectionAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	private int compoundsId;	// 当前小区id

	private String coordinates;	// 经纬度
	private int inspectionCategories;
	private String inspectionPointCode;
	private String inspectionProjectsStatus;
	@Resource
	private InspectionService inspectionService;

	/**
	 * 获取所有巡查项目类别
	 * 
	 * @return
	 */
	public String getAllInspectionCategories(){
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes<String, Object>(
					"code", 1).add("result", inspectionService.getAllInspectionCategories()));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

	public int getCompoundsId() {
		return compoundsId;
	}

	public String getCoordinates() {
		return coordinates;
	}

	public int getInspectionCategories() {
		return inspectionCategories;
	}

	public String getInspectionPointCode() {
		return inspectionPointCode;
	}

	/**
	 * 获取巡查项目
	 * 
	 * @return
	 */
	public String getInspectionProject() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes<String, Object>(
					"code", 1).add("result", inspectionService.getInspectionProject(
							inspectionCategories, inspectionPointCode)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}
	
	public String getInspectionProjectsStatus() {
		return inspectionProjectsStatus;
	}

	public void setCompoundsId(int compoundsId) {
		this.compoundsId = compoundsId;
	}

	public void setCoordinates(String coordinates) {
		this.coordinates = coordinates;
	}

	public void setInspectionCategories(int inspectionCategories) {
		this.inspectionCategories = inspectionCategories;
	}


	public void setInspectionPointCode(String inspectionPointCode) {
		this.inspectionPointCode = inspectionPointCode;
	}
	
	public void setInspectionProjectsStatus(String inspectionProjectsStatus) {
		this.inspectionProjectsStatus = inspectionProjectsStatus;
	}
	
	/**
	 * 提交巡查工单
	 * 
	 * @return
	 */
	public String subInspectionReport() {
		try {
			List<String> inspectionProjectsStatusList = JsonUtil.string2Obejct(inspectionProjectsStatus, List.class);
			int result = inspectionService.subInspectionTicket(((List<Employee>)request.getSession().getAttribute("users")).get(0).getId(), inspectionProjectsStatusList, coordinates, compoundsId);
			actionResult = ActionResult.getActionResult(new OpAttributes<String, Integer>(
					"code", result));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult
					.getActionResult(new OpAttributes<String, Integer>("code",
							0));
		}
		return returnType();
	}

}
